package com.freesky.provider.zuul.fallback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 14:26 2018/12/10
 * @description:
 */
public class MyClientHttpResponse implements ClientHttpResponse {
    private Logger logger = LoggerFactory.getLogger(MyClientHttpResponse.class);
    private String routeName ;

    public MyClientHttpResponse(String routeName ) {
        this.routeName = routeName;
    }


    /**
     * 服务调用失败后返回
     * @return
     * @throws IOException
     */
    @Override
    public InputStream getBody() throws IOException {
        logger.error(String.format("被代理服务[%s]故障，此结果由Zuul Gateway静态提供！",routeName));
        return new ByteArrayInputStream(String.format("被代理服务[%s]故障，此结果由Zuul Gateway静态提供！",routeName).getBytes());
    }

    @Override
    public HttpHeaders getHeaders() {
        HttpHeaders headers = new HttpHeaders() ;
        headers.set("Content-Type", "text/html; charset=UTF-8");
        return headers;
    }

    @Override
    public HttpStatus getStatusCode() throws IOException {
        return HttpStatus.BAD_REQUEST;
    }

    @Override
    public int getRawStatusCode() throws IOException {
        return HttpStatus.BAD_REQUEST.value();
    }

    @Override
    public String getStatusText() throws IOException {
        return HttpStatus.BAD_REQUEST.getReasonPhrase();
    }
    @Override
    public void close() {

    }
}
